package com.kly.smartbi.controller.bi;

import cn.hutool.json.JSONUtil;
import com.kly.smartbi.common.BaseResponse;
import com.kly.smartbi.common.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author admin
 * @date 2025/4/1
 */
@RequestMapping("/queue")
@RestController
@Slf4j
@Deprecated
@Profile({"dev", "local"})
public class QueueController {

    @Resource
    private ThreadPoolExecutor threadPoolExecutor;

    @PostMapping("/add")
    public BaseResponse<String> addQueue(String name) {
        CompletableFuture.runAsync(() -> {
            log.info("name:{} threadName: {}", name, Thread.currentThread().getName());
            try {
                TimeUnit.SECONDS.sleep(600);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, threadPoolExecutor);
        return ResultUtils.success("添加成功" + name);
    }

    /**
     * 获取线程池信息
     */
    @GetMapping("/info")
    public BaseResponse<String> getQueueInfo() {
        Map<String, Object> map = new HashMap<>();
    //     获取线程池信息
        map.put("activeCount", threadPoolExecutor.getActiveCount());
        map.put("maximumPoolSize", threadPoolExecutor.getMaximumPoolSize());
        map.put("queueSize", threadPoolExecutor.getQueue().size());
        map.put("taskCount", threadPoolExecutor.getTaskCount());
        return ResultUtils.success(JSONUtil.toJsonStr(map));
    }
}
